package com.cskaoyan.controller.wx_search;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.MemoryBean;
import com.cskaoyan.bean.vo.wx_search.WxSearchIndexVO;
import com.cskaoyan.service.wx_search.WxSearchService;
import com.cskaoyan.util.common.Constant;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: 冯子靖
 * @Description:
 * @Date: 2022/7/20 20:19
 */
@RestController
@RequestMapping("wx/search")
public class WxSearchController {

    @Autowired
    WxSearchService wxSearchService;

    @Autowired
    MemoryBean memoryBean;

    @GetMapping("index")
    public BaseRespVo printIndex(@RequestHeader(Constant.WX_TOKEN_HEADER) String token) {
        Integer userId = getUserId(token);
        WxSearchIndexVO wxSearchIndexVO = wxSearchService.printIndex(userId);
        return BaseRespVo.ok(wxSearchIndexVO);
    }

    @GetMapping("helper")
    public BaseRespVo printHelper(String keyword) {
        //需求,模糊查询,查找热门(数值1),按更新时间降序,deleted = 0
        List<String> helperList = wxSearchService.printHelperFromKeyword(keyword);
        return BaseRespVo.ok(helperList);
    }

    @PostMapping("clearhistory")
    public BaseRespVo clearHistory(@RequestHeader(Constant.WX_TOKEN_HEADER) String token) {
        Integer userId = getUserId(token);
        wxSearchService.clearHistory(userId);
        return BaseRespVo.ok();
    }





    private Integer getUserId(String token) {
        Integer userId = memoryBean.tokenAndUserId.get(token);
        if (userId == null) {
            return -1;
        }
        return userId;
    }
}
